<template>
  <div style="padding: 10px 10px 10px">
    <div class="follow_left">
      <a-menu
        v-model:openKeys="openKeys"
        v-model:selectedKeys="selectedKeys"
        v-if="isSelf"
        style="width: 114px"
        mode="vertical"
        @click="handleGroupSelect"
      >
        <a-menu-item key="-1">
          <template #icon><MailOutlined /></template>全部关注
        </a-menu-item>
        <div v-for="group in groupList" :key="group.id">
          <a-menu-item :key="group.id">
            <template #icon><CalendarOutlined /></template>
            {{ group.name }}
          </a-menu-item>
        </div>
        <a-menu-item key="-2" @click="addshowModal">
          <template #icon ><CalendarOutlined /></template>+
          添加分组
          <a-modal v-model:visible="addvisible" title="增添" @ok="createGroup">
            <!--自定义组件-->
            <div>
              <div class="laitem">
                <div class="latitle">
                  <span>分组名:</span>
                </div>
                <div class="laname-input">
                  <a-input v-model:value="groupName" placeholder="分组名" />
                </div>
              </div>
            </div>
            <!--自定义组件-->
          </a-modal>
        </a-menu-item>
      </a-menu>
    </div>

    <div class="follow_right" v-if="followList">
      <div
        style="
          height: auto;
          display: flex;
          box-shadow: 0px 0px 15px -10px gray;
          margin-top: 10px;
        "
        v-for="follow in followList"
        :key="follow.id"
      >
        <a :href="'/user/'+follow.account">
          <div class="followPhoto">
            <a-avatar
                :size="50"
                style="padding: 4px"
                :src="'/api/assets/avatar/' + follow.avatar"
            >
              <template #icon><UserOutlined /></template>
            </a-avatar>
          </div>
        </a>

        <div class="followInfo">
          <li class="followName">
            <a :href="'/user/'+follow.account" style="color: #ff5722">
              {{ follow.account }}
            </a>
          </li>
          <li class="followBiog">
            <span>{{ follow.intro }}</span>
          </li>
        </div>

        <div class="followButton" v-if="isSelf">
          <a-dropdown>
            <template #overlay>
              <a-menu>
                <a-menu-item key="1" @click="showFollowInGroup(follow.id)">设置分组</a-menu-item>
                <a-modal
                  v-model:visible="followInGroupVisiable"
                  title="设置分组"
                  @ok="handleSetGroup(follow.id)"
                >

                  <!--自定义组件-->
<!--                  <div v-for="group in groupOptions" :key="group.value">-->
<!--                    <div class="setgroups">-->
<!--                      <a-checkbox v-model:checked="group.value">{{group.label}}</a-checkbox>-->
<!--                    </div>-->
<!--                  </div>-->
                  <a-checkbox-group
                      v-model:value="selectedGroups"
                      :options="groupOptions"
                  />
                  <!--自定义组件-->
                </a-modal>
                <a-menu-item key="2"><a :href="'/chat?userId='+follow.id">发消息</a></a-menu-item>
                <a-menu-item key="3" @click="unFollowUser(follow.num)"
                  >取消关注</a-menu-item
                >
              </a-menu>
            </template>
            <a-button>
              已关注
              <DownOutlined />
            </a-button>
          </a-dropdown>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {computed, defineComponent, reactive, ref, toRefs} from "vue";
import { post, get } from "@/common";
import { useRouter } from "vue-router";
import {
  MailOutlined,
  CalendarOutlined,
  AppstoreOutlined,
  SettingOutlined,
} from "@ant-design/icons-vue";

export default defineComponent({
  data() {
    const target = reactive({
      id: null,
      num: null,
      mail: null,
      name: null,
      avatar: null,
      reg: null,
      state: null,
      sex: null,
      intro: null,
      area: null,
      birth: null,
    });
    const visitor = reactive({
      u_id: null,
      u_account: null,
      u_name: null,
      u_avatar: null,
    });
    const router = useRouter(); //获取路由器
    return {
      isSelf: computed(()=>{
        if(visitor===null)
          return false;
        else
          return visitor.u_account===target.num;
      }),
      visitor,
      followInGroupVisiable: ref(false),
      router,
      target,
      groupList: reactive([]),
      followPage: reactive([]),
      followList: reactive({}),
      selectedGroups: reactive([]),
      selectedUsers: reactive([]),
      groupOptions: reactive([]),
      query: reactive({
        pageNum: 1,
        pageSize: 5,
      }),
      groupInfo: reactive({}),
      groupName: ref()
    };
  },

  methods: {
    fetchRecentFollow() {
      get("/follow/recent", { account: this.$route.params.account }).then(
        (res) => {
          this.followList = res.data;
        }
      );
    },
    fetchGroupList() {
      get("/follow/group").then((res) => {
        this.groupList = res.data;
        this.groupOptions = res.data.map((group)=>{return {label: group.name, value: group.id}});
      });
    },
    fetchAllFollow() {
      get("/follow/group/all", this.query).then((res) => {
        this.followPage = res.data;
        this.followList = res.data.list;
      });
    },
    fetchUserInGroup(_group) {
      get("/follow/group/" + _group, this.query).then((res) => {
        this.followPage = res.data;
        this.followList = res.data.list;
      });
    },
    moveUserToGroup(group) {
      post("/follow/group/" + group + "/add/ByCopy", this.selectedUsers).then(
        (res) => {
          console.log(res.data);
        }
      );
    },
    getGroupsOfFollow(uid) {
      get("/follow/user/" + uid + "/group").then((res) => {
        console.log(res.data);
        this.selectedGroups = res.data.map((group)=>{return group.id});
      });
    },
    editFollowToGroup(uid) {
      post("/follow/user/" + uid + "/group", this.selectedGroups).then((res) => {
        console.log(res.data);
      });
    },
    delGroup(group) {
      get("/follow/group/" + group + "/del").then((res) => {
        console.log(res.data);
      });
    },
    createGroup() {
      post("/follow/group", {name: this.groupName}).then((res) => {
        this.groupList.push(res.data);
        this.addvisible=false;
      });
    },
    getCurrentUser() {
      get("/test/security").then((res) => {
        if (res != null) {
          this.visitor = res.data;
        }
      });
    },
    getTargetUser() {
      get("/user/" + this.$route.params.account + "/brief").then((res) => {
        if (res != null) {
          this.target = res.data;
        }
      });
    },
    checkForRender() {
      if (this.visitor.u_account === this.target.num) {
        this.fetchGroupList();
        this.fetchAllFollow();
      } else {
        this.fetchRecentFollow();
      }
    },
    handleGroupSelect(menuInfo) {
      const _t = String(menuInfo.key);
      if (_t === "-1") {
        this.fetchAllFollow();
      } else if (_t === "-2") {
        console.log(_t);
      } else {
        this.fetchUserInGroup(String(_t));
      }
    },
    unFollowUser(_account) {
      post("/user/" + _account + "/unfollow").then((res) => {
        if (res != null) {
          this.target = res.data;
        }
      });
    },
    handleSetGroup(e) {
      this.editFollowToGroup(e);
      this.followInGroupVisiable = false;
    },
    showFollowInGroup(e) {
      this.followInGroupVisiable = true;
      this.getGroupsOfFollow(e);
    }
  },
  setup() {
    const visible = ref(false);
    const addvisible = ref(false);

    const addshowModal = () => {
      addvisible.value = true;
    };

    const addhandleOk = (e) => {
      addvisible.value = false;
    };

    const state = reactive({
      selectedKeys: [],
      openKeys: [],
    });

    return {
      ...toRefs(state),
      visible,
      addvisible,
      addshowModal,
      addhandleOk,
    };
  },
  mounted() {
    this.getCurrentUser();
    this.getTargetUser();
    this.checkForRender();
  },
});
</script>

<style>
.follow_left {
  width: 15%;
  height: 100px;
  float: left;
  position: relative;
}

.follow_right {
  width: 80%;
  height: auto;
  float: left;
  min-height: 295px;
  position: relative;
  background: rgba(0, 0, 0, 0.007);
  margin-left: 10px;
}

.left_top {
  background-color: rgb(238, 238, 238);
  height: 50px;
}

.left_top_button {
  width: 80px;
  height: 50px;
  float: left;
  display: flex;
  align-items: center;
  justify-content: center;
}
.followPhoto {
  width: 100px;
  height: 100px;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.followInfo {
  width: 500px;
  height: 100px;
  padding: 4px;
  list-style-type: none;
  padding: 22px 10px 10px;
}

.followName {
  color: #ff5722;
  font-size: 14px;
  font-weight: normal;
  margin-right: -2px;
  overflow: hidden;
  vertical-align: -2px;
}

.followBiog {
  color: #000;
  font-size: 14px;
  margin-top: 10px;
  line-height: 18px;
}

.followButton {
  display: flex;
  align-items: center;
  padding: 10px 0px 10px;
  margin-right: 15px;
}

.setgroups{
  font-size: 17px;
  padding: 10px 10px 10px;
}
</style>



